<?php

/**
 * 摄像头 同步门店
 */

namespace App\Kuafu\Shop\Service;

use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Shop\Enum\CodeEnum;
use App\Kuafu\Shop\Model\CameraModel;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Service\CameraLibrary\YlkCamera;
use Illuminate\Support\Str;

class ShopCameraSyncService extends BaseService
{
    public function syncYlkCameraSupplierId(int $createAt = 0, int $assignShopCode = 0)
    {
        // 获取所有组
        $cam = new YlkCamera(null);

        // 获取组内的所有的门店列表
        $insertList = [];
        // 获取对应的门店数据
        $shopList = Shop::query()->get()->keyBy('shopCode')->toArray();
        $stores   = $cam->visibleStores(0);
        foreach ($stores as $store) {

            // 判断门店安装时间 是否小于 createAt
            if (strtotime($store['create_time']) < $createAt) {
                continue;
            }
            // 截取前四位 判断是否是 四位数字
            $shopCode = substr($store['store'], 0, 4);
            if (is_numeric($shopCode)) {
                $shopCode = (int)$shopCode;

                // 固定 shopCode
                if ($assignShopCode != 0 && $shopCode != $assignShopCode) {
                    continue;
                }

                if (empty($shopList[$shopCode])) {
                    // TODO 记录异常
//                    dump($shopCode . '不存在');
                    continue;
                }

                $insertList[$shopCode] = [
                    'cameraSupplierId' => $store['store_id'],
                    'cameraSupplier'   => CameraModel::SUPPLIER_YLK
                ];
            }

        }

        foreach ($insertList as $key => $value) {
            $affectRow = Shop::query()->where('shopCode', $key)->update($value);
        }
    }

    public function syncYlkCamera(int $shopId, string $storeId): ?int
    {
        $cam = new YlkCamera(null);
        if (empty($storeId)) {
            return 0;
        }

        $cameras = $cam->storeDeviceChannelList($storeId);
        if ($shopId == 15137) {
            foreach ($cameras as $key => $camera) {
                if (!in_array($camera['alias'], [
                    '门店储藏区',
                    '门店客座区',
                    '门店操作区',
                    '门店客流',
                    '室外外摆区２',
                    '室外外摆区',
                ])) {
                    unset($cameras[$key]);
                }
            }
        }

        $insertList = [];
        foreach ($cameras as $camera) {
            $type = Str::contains($camera['alias'],
                ['客流']) ? CodeEnum::SHOP_CAMERA_TYPE_1 : CodeEnum::SHOP_CAMERA_TYPE_2;
            // 配置入库的列表
            $insertList[] = [
                'serialNo'     => $camera['nvr_sn'] ?: $camera['device_id'] ?: 0,
                'productKey'   => $camera['nvr_channel_id'] ?: $camera['channel_id'] ?: 0,
                'tripleType'   => 0,
                'name'         => $camera['alias'],
                'deviceName'   => $camera['device_auto_id'],
                'deviceSecret' => $camera['device_id'],
                'mac'          => $camera['channel_id'],
                'shopId'       => $shopId,
                'supplier'     => CameraModel::SUPPLIER_YLK,
                'createdAt'    => time(),
                'updatedAt'    => time(),
                'type'         => $type
            ];
        }

        if (count($insertList) > 0) {
            CameraModel::query()->where('supplier', CameraModel::SUPPLIER_YLK)
                       ->where('shopId', $shopId)->delete();
            CameraModel::query()->insert($insertList);
        }

        return count($insertList);
    }
}
